package com.td.erhome.utils;

import android.content.SharedPreferences;
import android.util.Base64;

import com.td.erhome.App;
import com.td.erhome.models.User;

/**
 * Created by MagicBean on 2016/03/05 14:14:53
 */
public class UserManager {
    private static UserManager mUserManager;

    private boolean isRegisterToServer;

    private UserManager() {

    }

    public static UserManager getIns() {
        if (mUserManager == null) {
            synchronized (UserManager.class) {
                mUserManager = new UserManager();
            }
        }
        return mUserManager;
    }
    /**
     * 获取用户信息
     *
     * @param
     */

    public synchronized User getUser() {
        SharedPreferences mPreferences = App.getInstance().getSharedPreferences("UserInfo", 0);
        String temp = mPreferences.getString("_user", "");
        byte[] base64Bytes = Base64.decode(temp, Base64.DEFAULT);
        String retStr = new String(base64Bytes);
        return JsonParser.deserializeByJson(retStr, User.class);
    }

    /**
     * 保存用户信息
     * @param user
     */
    public synchronized void saveUserInfo(User user) {
        if (user == null) return;
        SharedPreferences mPreferences = App.getInstance().getSharedPreferences("UserInfo", 0);
        String json = JsonParser.serializeToJson(user);
        String temp = new String(Base64.encode(json.getBytes(), Base64.DEFAULT));
        mPreferences.edit().putString("_user", temp).commit();
    }
    /**
     * 清楚用户信息
     */

    public synchronized void clearUserInfo() {
        SharedPreferences mPreferences = App.getInstance().getSharedPreferences("UserInfo", 0);
        mPreferences.edit().putString("_user", "").commit();
    }

    /**
     *
     * @param
     */
    public synchronized void saveUserPer(int per) {
        SharedPreferences mPreferences = App.getInstance().getSharedPreferences("per", 0);
        mPreferences.edit().putInt("per", per).commit();
    }

    public synchronized int getPer() {
        SharedPreferences mPreferences = App.getInstance().getSharedPreferences("per", 0);
        return  mPreferences.getInt("per", 0);
    }

    public synchronized void clearPer() {
        SharedPreferences mPreferences = App.getInstance().getSharedPreferences("per", 0);
        mPreferences.edit().putInt("per", 0).commit();
    }

}
